package utility;

import java.io.File;
import java.io.RandomAccessFile;

public class ReadFile 
{
//	String MasterPath = Input.MASTERREPORTSPATH;
	//String fileFormat = ".txt";
	
	public String searchObject(String buffer[], String object)
	{
		
		try
		{
			
			for(int i=0;i<buffer.length;i++)
			{
				String so = buffer[i];
				//System.out.println("utility::ReadFile::searchObject():: The Line is : "+so);
				if(so.contains(object))
				{
					int index = so.indexOf(object);
					int end;
					end = so.indexOf("\t",index+1);
					if(object.equals(so.substring(index, end)))
					{
						System.out.println("utility::ReadFile::searchObject():: Object Found The Line is : "+so);
						return so;
					}
				}
			}
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		
		return "error";
	}
	
	public StringBuffer searchObject(String[] buffer, String object, String SplitChar)
	{
		StringBuffer Result = new StringBuffer();
		System.out.println("reportManager::AutoUpdate::searchObject():: -> Objet : "+object);
		RandomAccessFile fRead ;
		
		try
		{
			for(int i=0;i<buffer.length;i++)
			{
				String so = buffer[i];
				//System.out.println("utility::ReadFile::searchObject():: The Line is : "+so);
				if(so.contains(object))
				{
					int index = so.indexOf(object);
					int end;
					end = so.indexOf("\t",index+1);
					if(object.equals(so.substring(index, end)))
					{
						System.out.println("utility::ReadFile::searchObject():: Object Found The Line is : "+so);
						Result.append(so+SplitChar);
					}
				}
			}
			System.out.println("utility::ReadFile::searchObject():: Object Not Found The Line is : ");
			
			
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		if(Result.length() <1 )
			return new StringBuffer("error");
		return Result;
	}
	
	public String searchObject(String fileName, StringBuffer object,String splitChar)
	{
		System.out.println("reportManager::AutoUpdate::searchObject():: -> File Name : "+fileName+"\nObjet : "+object);
		boolean FLAG = false;
		System.out.println("reportManager::AutoUpdate::searchObject():: -> Objet : "+object);
		RandomAccessFile fRead ;
		
		String[] ObjectArray = object.toString().split(splitChar);
		File masterFile = new File(fileName);
		if(masterFile.exists() == false)
		{
			System.out.println("reportManager::AutoUpdate::searchObject():: -> File Not Found");
			return "error";
		}
		try
		{
			String so = null;
			fRead = new RandomAccessFile(masterFile,"r");
			while((fRead.read())!=-1)
			{
				fRead.seek(fRead.getFilePointer()-1);
				so = fRead.readLine();
				String[] fileArray = so.split("\t"); 
				       
				System.out.println("utility::ReadFile::searchObject():: The Line is "+so);
				for(int j=0; j<ObjectArray.length;j++)
				{
					for(int i = 0; i<fileArray.length; i++)
					{
						if(fileArray[i].equals(ObjectArray[j]))
						{
							FLAG = true;
								break;
						}
						else
							FLAG = false;
					}
					if(FLAG == true)
						continue;
					FLAG = false;
				}
				if(FLAG == true)
				{
					System.out.println("utility::ReadFile::searchObject():: Object Found The Line is : "+so);
					fRead.close();
					return so;
				}
			}
			
			System.out.println("utility::ReadFile::searchObject():: Object Not Found The Line is : ");
			
			fRead.close();
			return "error";
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return "error";
	}
	
	public String getMasterReportPath()
	{
		return "";
	}
	
	public String getFileFormat()
	{
		return Input.FILRFORMAT;
	}
	
	public static void main(String a[])
	{
		ReadFile rf = new ReadFile();
		//rf.searchObject("C://Reports//Basic_Reports//Master_Reoprts//833//833_mid.txt", new StringBuffer("815!83"),"!");
		//System.out.println(rf.searchObject("/home/divyesh/Reports/Basic_Reports/Master_Reports/9/9_mid.txt","9","!").toString());
		//rf.searchObject("C://Reports//Basic_Reports//Master_Reoprts//833//833_mid.txt","833");
	}
	
}
